home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / Spinner.java < prev    next >
Text File  |  1998-09-15  |  3KB  |  127 lines

  1. /*
  2.  * @(#)Spinner.java    1.1 98/07/18
  3.  *
  4.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  */
  7.  
  8. package actual;
  9.  
  10. import java.applet.*;
  11. import java.lang.*;
  12. import java.util.*;
  13. import java.awt.*;
  14.  
  15. /**
  16.  * Spinner - a class that creates a lightweight component that
  17.  * shows a spinning wheel.
  18.  *
  19.  * Lightweight components can have "transparent" areas, meaning that
  20.  * you can see the background of the container behind these areas.
  21.  *
  22.  */
  23. public class Spinner extends Component {
  24.  
  25.   float percentDone = 0;
  26.   int totalTicks    = 60;
  27.   int currentTick   = 0;
  28.   
  29.   SpinnerThread spinnerThread;
  30.   
  31.   /**
  32.    * Constructs a Spinner
  33.    */
  34.   public Spinner() {
  35.       setForeground(Color.gray);
  36.       setForeground(Color.lightGray);
  37.   }
  38.   
  39.   /**
  40.    * paints the Spinner
  41.    */
  42.   public void paint(Graphics g) {
  43.       int start_angle = 90;
  44.       int done_angle = (int) (percentDone * 360);
  45.       
  46.       g.setColor(getBackground());
  47.       g.fillArc(3, 3, getSize().width-8, getSize().height-8, 0, 360);
  48.       
  49.       g.setColor(getForeground());
  50.       g.fillArc(3, 3, getSize().width-8, getSize().height-8, start_angle, done_angle);
  51.  
  52.       g.setColor(Color.black);
  53.       g.drawArc(3, 3, getSize().width-8, getSize().height-8, 0, 360);
  54.   }
  55.  
  56.   public void setCurrentTick(int tick) {
  57.       currentTick = tick;
  58.  
  59.       if(currentTick > totalTicks) {
  60.       percentDone = 1;
  61.       } else if(currentTick == 0) {
  62.       percentDone = 0;
  63.       } else {
  64.       percentDone = (float) currentTick / (float) totalTicks;
  65.       }
  66.       
  67.       // Repaint might flicker a bit. To avoid this, you can use
  68.       // double buffering (see the Gauge example).
  69.       repaint();
  70.   }
  71.  
  72.   public void startSpinning() {
  73.       spinnerThread = new SpinnerThread(this);
  74.       spinnerThread.start();
  75.   }
  76.  
  77.   public void stopSpinning() {
  78.       spinnerThread.stop();
  79.       spinnerThread = null;
  80.   }
  81.  
  82.   public void setTotalTicks(int tick) {
  83.       totalTicks = tick;
  84.   }
  85.  
  86.   public int getTotalTicks() {
  87.       return totalTicks;
  88.   }
  89.  
  90.   public int getCurrentTick() {
  91.       return currentTick;
  92.   }
  93.  
  94.  
  95. }
  96.  
  97.  
  98.  
  99. /**
  100.  * SpinnerThread: spins the wheel
  101.  */
  102. class SpinnerThread extends Thread {
  103.  
  104.   Spinner spinner;
  105.  
  106.   SpinnerThread(Spinner spinner) {
  107.       super("Spinner Thread");
  108.       this.spinner = spinner;
  109.   }
  110.  
  111.   public void run () {
  112.       int i = spinner.getCurrentTick();
  113.       while(true) {
  114.       try {
  115.           while (i-- > 0) {
  116.           spinner.setCurrentTick(i);
  117.           sleep(100);
  118.           }
  119.       } catch (java.lang.InterruptedException e) {
  120.           // don't care if we are interrupted
  121.       }
  122.       i = spinner.getTotalTicks();
  123.       }
  124.   }
  125. }
  126.  
  127.